package com.poisonh.unifiedauth.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.model.DevicesModel;

import java.util.ArrayList;
import java.util.List;


/**
 * 设备管理
 * Created by PoisonH on 2016/7/21.
 */


public class RvDeviceAdapter extends RecyclerView.Adapter<RvDeviceAdapter.MyViewHolder>
{
    private Context mContext;
    private List<DevicesModel> mDeviceLists;

    public RvDeviceAdapter(Context context)
    {
        this.mContext = context.getApplicationContext();
        mDeviceLists = new ArrayList<>();
    }

    @Override
    public int getItemCount()
    {
        return mDeviceLists.size();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_device_item, parent, false));
    }

    /**
     * 绑定视图
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position)
    {
        String type = mDeviceLists.get(position).getTYPE();
        if (type.equals("0"))
        {
            holder.mIvOs.setBackgroundResource(R.mipmap.icon_pc);
        }
        if (type.equals("1"))
        {
            holder.mIvOs.setBackgroundResource(R.mipmap.icon_android);
        }
        if (type.equals("2"))
        {
            holder.mIvOs.setBackgroundResource(R.mipmap.icon_iphone);
        }
        holder.mTvType.setText(mDeviceLists.get(position).getPHONE_MODEL());
        holder.mTvTime.setText(mDeviceLists.get(position).getTIME());
    }

    protected class MyViewHolder extends RecyclerView.ViewHolder
    {
        private TextView mTvType;
        private TextView mTvTime;
        private ImageView mIvOs;

        protected MyViewHolder(View view)
        {
            super(view);
            mTvType = (TextView) view.findViewById(R.id.tv_type);
            mTvTime = (TextView) view.findViewById(R.id.tv_time);
            mIvOs = (ImageView) view.findViewById(R.id.iv_os);
        }
    }

    /**
     * 添加数据集，并更新
     *
     * @param list
     */
    public void notif(List<DevicesModel> list)
    {
        this.mDeviceLists.addAll(mDeviceLists.size(), list);
        this.notifyDataSetChanged();
    }

    /**
     * 更新数据集
     *
     * @param list
     */
    public void update(List<DevicesModel> list)
    {
        if (mDeviceLists.size() > 0)
        {
            mDeviceLists.clear();
        }
        this.mDeviceLists.addAll(list);
        this.notifyDataSetChanged();
    }


}
